package com.zmm.product.service.impl;

import com.zmm.common.base.model.ResultCode;
import com.zmm.common.enums.RecordType;
import com.zmm.common.exception.handle.BusinessException;
import com.zmm.product.dao.ProductDao;
import com.zmm.product.model.Product;
import com.zmm.product.service.ProductService;
import com.zmm.product.vo.ProductVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 900045
 * @description:
 * @name ProductServiceImpl
 * @date By 2021-07-07 15:20:44
 */
@Service
public class ProductServiceImpl implements ProductService {
	
	@Resource
	private ProductDao productDao;


	/**
	 * 新增商品业务 -- 实现
	 * @author: 900045
	 * @date: 2021-07-07 16:38:21
	 * @throws 
	 * @param product: 
	 * @return: boolean
	 **/
	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean addProduct(Product product) throws BusinessException{
		product.setCreateTime(System.currentTimeMillis());
		Integer insert = productDao.insertProduct(product);
		if (insert > 0){
			return true;
		}
		throw new BusinessException(ResultCode.INSERT_FAILED, RecordType.SAVE_PRODUCT,"addProduct");
	}


	@Override
	public Product findOneProductByProductId(Integer productId) {
		return productDao.selectOneProductByProductId(productId);
	}

	/**
	 * 审核商品状态业务 -- 实现
	 * @author: 900045
	 * @date: 2021-07-07 16:38:10
	 * @throws 
	 * @param auditState: 
	 * @param productId: 
	 * @return: boolean
	 **/
	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean modifyProductAuditStatus(Integer auditState, Integer productId) throws BusinessException{
		Integer update = productDao.updateProductAuditStatus(auditState, System.currentTimeMillis(), productId);
		if (update > 0){
			return true;
		}
		throw new BusinessException(ResultCode.UPDATE_FAILED, RecordType.MODIFY_PRODUCT_AUDIT_STATUS,"modifyProductAuditStatus");
	}

	/**
	 * 更新商品信息业务 -- 实现
	 * @author: 900045
	 * @date: 2021-07-07 16:37:49
	 * @throws 
	 * @param product: 
	 * @return: boolean
	 **/
	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean modifyProduct(Product product) throws BusinessException {
		Integer update = productDao.updateProduct(product);
		if (update > 0){
			return true;
		}
		throw new BusinessException(ResultCode.UPDATE_FAILED, RecordType.UPDATE_PRODUCT,"modifyProduct");
	}

	/**
	 * 根据商品ID 删除商品  -- 实现
	 * @author: 900045
	 * @date: 2021-07-07 16:44:11
	 * @throws 
	 * @param productId: 
	 * @return: boolean
	 **/
	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean removeProductByProductId(Integer productId) throws BusinessException {
		Integer delete = productDao.deleteProduct(productId);
		if (delete > 0){
			return true;
		}
		throw new BusinessException(ResultCode.DELETE_FAILED, RecordType.DELETE_PRODUCT,"removeProductByProductId");
	}


	/**
	 * 根据条件查询 商品列表 -- 实现
	 * @author: 900045
	 * @date: 2021-07-07 16:51:20
	 * @throws 
	 * @param productVo: 
	 * @return: java.util.List<com.zmm.product.model.Product>
	 **/
	@Override
	public List<Product> queryProductListByProductVo(ProductVo productVo) {
		return productDao.searchProductListByProductVo(productVo);
	}
}
